// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/base/api/MessageRecordApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/base/model/MessageRecordDto'
import EditClass from '@api/base/model/MessageRecordReadReq'

const OneClass = ListClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'messageRecordQuery', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'messageRecordDetail', DTO)

    const DTO2 = CORE.grpcDTO(EditClass, { messageId: id, isRead: 1 })
    const TIP_READ = TIP.replace(/(【.*】 ).*/, (_, $1) => `${$1}标记为已读`)
    await CORE.grpc(TIP_READ, CLIENT, 'messageRecordRead', DTO2)

    return resp
  },
  transOne(obj, { state: { one } }) {
    return { ...one, ...obj }
  },

  // —————————————————————————————— 修改 —————————————————————————————— //

  async edit({ id, isRead }) {
    const TIP = `${id ? '' : '全部'}标记为${isRead === 1 ? '已' : '未'}读`
    const DTO = CORE.grpcDTO(EditClass, { messageId: id, isRead })
    await CORE.grpc(TIP, CLIENT, 'messageRecordRead', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 通过 —————————————————————————————— //

  async pass({ id }) {
    const TIP = `${id ? '' : '全部'}标记为已读`
    const DTO = CORE.grpcDTO(EditClass, { messageId: id, isRead: 1 })
    await CORE.grpc(TIP, CLIENT, 'messageRecordRead', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 拒绝 —————————————————————————————— //

  async rej({ id }) {
    const TIP = `${id ? '' : '全部'}标记为未读`
    const DTO = CORE.grpcDTO(EditClass, { messageId: id, isRead: 2 })
    await CORE.grpc(TIP, CLIENT, 'messageRecordRead', DTO)
    return CORE.grpcOK(TIP)
  },
}
